<?php
session_start();
include("conexion.php");
$lugar=$_GET['lugar'];
$nombre=$_GET['nombre'];
mysql_select_db($dbname, $con);
$sql="SELECT id FROM usuarios WHERE nick='$nombre'";
$resultado=mysql_query($sql, $con);
While ($row=mysql_fetch_array($resultado)){
   $id=$row['id'];
} 
$_SESSION['id']=$id;
$_SESSION['nombre']=$nombre;
$sql="SELECT * FROM correo WHERE id_usuario='$id'";
$resultado=mysql_query($sql, $con);
While ($row=mysql_fetch_array($resultado)){
   $mail=$row['email'];
   $pwd=$row['password'];
} 
if(mysql_num_rows($resultado)==0){
	echo "<form action='insertarcorreo.php' method='POST'>
		  <input type='hidden' name='id' value='$id'>
		  <table><tr><td><span>EMAIL</span></td><td><input type='text' name='email' ></td></tr>
		  <tr><td><span>CONTRASEÑA</span></td><td><input type='password' name='pwd' ></td></tr>
		  <tr><td><input type='submit' id='insertar' value='Insertar Correo'></td><td></td></tr></table>
		  </form>";
}else{

	/* connect to gmail */
$hostname = '{imap.gmail.com:993/imap/ssl}INBOX';
$username = $mail;
$password = $pwd;

/* try to connect */
$inbox = imap_open($hostname,$username,$password) or die('Cannot connect to Gmail: ' . imap_last_error());

/* grab emails */
$emails = imap_search($inbox,'ALL');

/* if emails are returned, cycle through each... */
if($emails) {
  /* begin output var */
  $output = '';
  $count=0;
  $cant=2;
  $total_records= count($emails);
  $pages = ceil($total_records / $cant);
  $output.= "<form action='cambiar_correo.php' method='POST'>
  			 <input type='hidden' name='id' value='$id'>
  			 <input type='submit' id='cambiar_bot' value='Cambiar correo' >
			 </form>";
  /* put the newest emails on top */
  rsort($emails);
  /* for every email... */
  $output.= '<div class="zebra">';
  $output.= '<ul>';
  foreach($emails as $email_number) {
	  
	 if($count%2==0){
		$output.='<li class="zebra2">';	 
	 }else{
	 	$output.='<li class="zebra1">';	
	 }
	$count++;
    /* get information specific to this email */
    $overview = imap_fetch_overview($inbox,$email_number,0);
    $message = imap_fetchbody($inbox,$email_number,20);
    
    /* output the email header information */
	$output.= '<div class="cabeza_email" >';
    $output.= '<div class="toggler '.($overview[0]->seen ? 'read' : 'unread').'">';
	$output.= '<b>Asunto: </b>';
    $output.= '<span class="subject">'.$overview[0]->subject.'</span> ';
	$output.= '<br /><b>De: </b>';
    $output.= '<span class="from">'.$overview[0]->from.'</span>';
	$output.= '<br /><b>Fecha: </b>';
    $output.= '<span class="date">on '.$overview[0]->date.'</span>';
	$output.= '<button id="men_email_ver'.$email_number.'" onclick="mostrar_mensaje('.$email_number.');cambiar1('.$email_number.')" class=visible>+</button>';
	$output.= '<button id="men_email_oculto'.$email_number.'" onclick="cambiar2('.$email_number.')" class=oculto>-</button>' ;
	$output.= '<div id="carga"></div>';
    $output.= '</div>';
	$output.= '</div>';
	    
    /* output the email body */
	
	$output.= '<div id="email'.$email_number.'" style="display:block; background:#ffffff;">';
	$output.='</div>';
	$output.= '</li>';
	}
	$output.='</ul>';
	$output.='</div>';
	
	echo $output;
} 

/* close the connection */
imap_close($inbox);
}

?>

